'use strict';

app.controller('debunkerCtrl', ['$scope', '$http', '$stateParams', '$rootScope', '$modal', 'lodash','$location','$anchorScroll', '$timeout', function ($scope, $http, $stateParams, $rootScope, $modal, lodash,$location,$anchorScroll, $timeout) {
    $rootScope.initBreadcrumb($scope.breadcrumb);
    //页面加载时，给headerElement设置元素
    $rootScope.initHeaderElements($scope.settings);
    var backUrl = {
        show: true,
        label: "返回",
        func: function () {
            $rootScope.backHistory();
        }
    }
    $scope.settings.btnEle.push(backUrl);

    $scope.info = {
        debunkerList: [],
    };

    getAllDebunker();
    getReqName();
    setFlowHeight();

    function setFlowHeight() {
        $timeout(function() {        
            var h = document.documentElement.clientHeight || document.body.clientHeight;
            h = h - 155;
            $(".table-content").css({
                "max-height": h +"px",
                "overflow": "auto",
            });
        }, 500)
    }

    $(window).resize(function() {
        setFlowHeight();
    });

    //获取req名称
    function getReqName() {
        var url = matrix_product_url +"/brdRequirement/getBrdRequirements?withFeatureDesc=0&reqId="+ $stateParams.reqId;
        $http.get(url).success(function(result) {
            if(result.code == "0") {
                if(result.data) {
                    $scope.info.reqDetails = result.data[0];
                    $scope.breadcrumb.push('产品设计',$scope.info.reqDetails.reqName);
                }
            } else {
                $rootScope.toasterWarningInfo(result.desc);
            }
        });
    }

    // 获取全部数据
    function getAllDebunker() {
        var url = matrix_product_url + "/debunk/get?reqId="+ $stateParams.reqId;
        $http.get(url).success(function(result) {
            if(result.code == "0") {
                $timeout(function() {
                    $scope.info.debunkerList = result.data;
                }, 500);
            } else {
                $rootScope.toasterErrorInfo(result.desc);
            }
        });
    };

    $scope.update = function(debunker) {
        var url = matrix_product_url +"/debunk/save";
        debunker.isImprove = "0";
        var postParams = debunker;
        $http({
            method: "post",
            url: url,
            data: JSON.stringify(postParams),
            headers: {"Content-Type": "application/json"}
        }).success(function (result) {
            if (result.code == "0") {
                var crtDebunker = lodash.findWhere($scope.info.debunkerList, {id: debunker.id});
                crtDebunker.isImprove = 0;
                $rootScope.toasterSuccessInfo("操作成功！");
            } else {
                $rootScope.toasterErrorInfo("操作失败，请刷新重试！");
            }
        });
    }

}]);